package com.google.code.swingonfire.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

public class JBorderPanel extends JPanel
{

    private static final long serialVersionUID = -8054582310722067527L;

    private final Component component;
    private final JScrollPane scrollPane;

    /**
     * Creates a panel with an empty border
     * 
     * @param component the component
     * @param topAndBottom the top and bottom border width
     * @param leftAndRight the left and right border width
     */
    public JBorderPanel(Component component, int topAndBottom, int leftAndRight)
    {
        this(component, BorderFactory.createEmptyBorder(topAndBottom, leftAndRight, topAndBottom, leftAndRight));
    }

    /**
     * Creates a panel with an empty border
     * 
     * @param component the component
     * @param top the top border width
     * @param left the left border width
     * @param bottom the bottom border width
     * @param right the right border width
     */
    public JBorderPanel(Component component, int top, int left, int bottom, int right)
    {
        this(component, BorderFactory.createEmptyBorder(top, left, bottom, right));
    }

    /**
     * Creates a panel width a line border in the specified color
     * 
     * @param component the component
     * @param color the color
     */
    public JBorderPanel(Component component, Color color)
    {
        this(component, color, 1);
    }

    /**
     * Creates a panel width a line border in the specified color
     * 
     * @param component the component
     * @param color the color
     * @param thickness the tickness of the line border
     */
    public JBorderPanel(Component component, Color color, int thickness)
    {
        this(component, BorderFactory.createLineBorder(color, thickness));
    }

    /**
     * Creates a panel with the specified border
     * 
     * @param component the component
     * @param border the border
     */
    public JBorderPanel(Component component, Border border)
    {
        this(component, border, false);
    }

    /**
     * Creates a panel with the specified border
     * 
     * @param component the component
     * @param border the border
     * @param scroll true to add a scrollpane (without a border)
     */
    public JBorderPanel(Component component, Border border, boolean scroll)
    {
        super(new BorderLayout());

        this.component = component;

        if (border != null)
        {
            setBorder(border);
        }

        setOpaque(false);

        if (scroll)
        {
            scrollPane = new JScrollPane(component);
            scrollPane.setBorder(BorderFactory.createEmptyBorder());
            add(scrollPane, BorderLayout.CENTER);
        }
        else
        {
            scrollPane = null;
            add(component, BorderLayout.CENTER);
        }
    }

    public Component getComponent()
    {
        return component;
    }

    public JScrollPane getScrollPane()
    {
        return scrollPane;
    }

}
